<?php

require_once ('Logger/Appender/Abstraction.php');

class Logger_Appender_File extends Logger_Appender_Abstraction {
	protected $_options    =   array(
	'file'=>'',
	'buffer'=>0,
	);
	protected function _append(Logger_Event $event) {
		$msg = $this->_layout->format ( $event );
		$this->_bufferSize += strlen ( $msg );
		$this->_logBuffer [] = $msg;
		
		if ($this->_bufferSize >= $this->_options['buffer'])
			$this->_flush ();
	}
	protected function _flush() {
	    
		$file = $this->_options ['file'];
		$dir = dirname ( $file );
		if (! is_dir ( $dir )) {
			
			if ( !mkdir ($dir,0755,true ) )
				throw new Logger_Excetion('日志目录不存在');
			
		}
		
		$fp = fopen ( $file, 'a' );
		
		
		$ret = fwrite ( $fp, implode ( "\n", $this->_logBuffer )."\n" );
		fclose ( $fp );
		if ($ret)
		{
		    $this->_logBuffer     =   array ();
		    $this->_bufferSize    =   0;
		}
			
	
	}
	
	protected function _close()
	{
	    
	}

}

?>